﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Glucometer
{
    public class ErrorCodeResolver
    {
        //THE ERROR CODE IS A 3 DIGIT NUMBER.
        //THE FIRST DIGIT REPRESENTS THE MODE AND THE SECOND AND THIRD DIGIT REPRESENTS ERROR
        //NEGATIVE ERROR MEANS UNKNOWN ERROR
        public static int GetErrorCode(ErrorCode code, OperationMode mode)
        {
            int modeid;

            switch(mode)
            {
                case OperationMode.BROWSING:
                    modeid = 4;
                    break;
                case OperationMode.SETUP:
                    modeid = 2;
                    break;
                case OperationMode.TESTING:
                    modeid = 3;
                    break;
                case OperationMode.UPLOADING:
                    modeid = 5;
                    break;
                default: 
                    modeid = 1;
                    break;
            }

            int errorcode = 0;
            switch (code)
            { 
                case ErrorCode.INITIALIZATION_ERROR:
                    errorcode = (modeid * 100) + 1;
                    break;
                case ErrorCode.INSUFFICIENTBLOOD_ERROR:
                    errorcode = (modeid * 100) + 5;
                    break;
                case ErrorCode.INVALIDSTRIP_ERROR:
                    errorcode = (modeid * 100) + 4;
                    break;
                case ErrorCode.METERSTATUSCHECK_ERROR:
                    errorcode = (modeid * 100) + 2;
                    break;
                case ErrorCode.MODE_VALIDATION_ERROR:
                    errorcode = (modeid * 100) + 3;
                    break;
                case ErrorCode.NONE:
                    errorcode = (modeid * 100) + 7;
                    break;
                case ErrorCode.TESTING_ERROR:
                    errorcode = (modeid * 100) + 6;
                    break;
                default:
                    errorcode = (modeid * 100) - 1; //UNKNOWN ERROR
                    break;
            }

            return errorcode;
        }

        public static string GetDescription()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("-->THE ERROR CODE IS A 3 DIGIT NUMBER.");
            builder.AppendLine("-->THE FIRST DIGIT REPRESENTS THE MODE AND THE SECOND AND THIRD DIGIT REPRESENTS ERROR.");
            builder.AppendLine("-->NEGATIVE ERROR MEANS UNKNOWN ERROR.");
            builder.AppendLine("********DESCRIPTION FOR FIRST DIGIT********");
            builder.AppendLine("1 => NONE Mode");
            builder.AppendLine("2 => Setup Mode");
            builder.AppendLine("3 => Testing Mode");
            builder.AppendLine("4 => Browsing Mode");
            builder.AppendLine("5 => Uploading Mode");
            builder.AppendLine("********DESCRIPTION FOR SECOND AND THIRD DIGIT********");
            builder.AppendLine("01 => Initialization Error");
            builder.AppendLine("02 => Status Check Error"); 
            builder.AppendLine("03 => Mode Validation Error");
            builder.AppendLine("04 => Invalid Strip Error");
            builder.AppendLine("05 => Insufficient Blood Error");
            builder.AppendLine("06 => Testing Error");
            builder.AppendLine("07 => None");
            builder.AppendLine("********DESCRIPTION FOR UNKNOWN ERROR********");
            builder.AppendLine("01 => Unknown Error");
            return builder.ToString();
        }
    }
}
